package com.ty.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageInfo;
import com.ty.common.utils.R;
import com.ty.entity.Customer;
import com.ty.entity.Orders;
import com.ty.service.CustomerService;
import com.ty.service.OrdersService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.io.Serializable;
import java.util.Date;
import java.util.List;

/**
 * (Orders)表控制层
 *
 * @author makejava
 * @since 2020-12-01 22:17:53
 */
@RestController
@RequestMapping("orders")
@Slf4j
public class OrdersController extends ApiController {
    /**
     * 服务对象
     */
    @Resource
    private OrdersService ordersService;
    @Autowired
    private CustomerService customerService;


    /**
     * 修改订单信息
     * @param customer
     * @param orders
     * @return
     */
    @PostMapping("updateOrder")
    public R updateOrder(
            Customer customer, Orders orders
    ){
        log.info("修改订单传递数据为; {}, {}", customer, orders);
        this.customerService.updateById(customer);
        orders.setUpdtetime(new Date());
        this.ordersService.updateById(orders);
        return R.ok();
    }

    /**
     * 修改订单状态
     * @param orders
     * @return
     */
    @PostMapping("updateStatus")
    public R updateStatus(
            Orders orders
    ){
        this.ordersService.updateById(orders);
        return R.ok();
    }

    /**
     * 解除订单
     * @param id
     * @return
     */
    @PostMapping("dismiss")
    public R dismissOrder(
            @RequestParam(value = "id") Integer id
    ){
        Orders orders = new Orders();
        orders.setId(id);
        orders.setMid(0);
        orders.setStatus("0");
        this.ordersService.updateOrder(orders);
        return R.ok();
    }

    /**
     * 指派订单
     * @param id
     * @param mid
     * @return
     */
    @PostMapping("dispatch")
    public R dispatchOrder(
            @RequestParam(value = "id") Integer id,
            @RequestParam(value = "mid") Integer mid
    ){
        Orders orders = new Orders();
        orders.setId(id);
        orders.setMid(mid);
        orders.setStatus("1");
        this.ordersService.updateById(orders);
        return R.ok();
    }

    /**
     * 添加订单
     * @param customer
     * @param orders
     * @return
     */
    @PostMapping("save")
    public R saveOrder(Customer customer, Orders orders){
        log.info("传递的参数为,{},{}", customer, orders);

        //查看是否存在该用户 =0 则不已存在
        if (this.customerService.count(new QueryWrapper<Customer>().eq("phone", customer.getPhone())) == 0){
            this.customerService.saveCustomer(customer);
        }
        //查看该用户是否有订单未完成
        // true 代表没有未完成的订单
        if (this.ordersService.isCompletedOrderByPhone(orders.getPhone())){
            boolean saveOrderCustoner = this.ordersService.saveOrderCustoner(customer, orders);
            //true 代表添加成功
            if (saveOrderCustoner) return R.ok();
        }
        return R.error();
    }

    @GetMapping(value = "getOrdersByMobile")
    public R  getOrder(
            HttpSession session
    ){
        //获取以登录用户的手机号
        String mobile = (String) session.getAttribute("user");
        List<Orders> orders = this.ordersService.queryByMobileIsOrdersCustomer(mobile);
        return R.ok().put("orders", orders);
    }

    @PostMapping("page")
    public R getPage(
            Orders orders,
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "limit", defaultValue = "10") Integer limit
    ){
        log.info("分页查询的数据为{},{},{}", orders, page, limit);
        PageInfo<Orders> pageInfo = this.ordersService.getPage(orders, page, limit);
        return R.ok().put("page", pageInfo);
    }

    /**
     * 分页查询所有数据
     *
     * @param page   分页对象
     * @param orders 查询实体
     * @return 所有数据
     */
    /*@GetMapping
    public R selectAll(Page<Orders> page, Orders orders) {
        return R.ok().put("orders", this.ordersService.page(page, new QueryWrapper<>(orders)));
    }*/

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public R selectOne(@PathVariable Serializable id) {
        return R.ok().put("order", this.ordersService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param orders 实体对象
     * @return 新增结果
     */
    @PostMapping
    public R insert(
            Orders orders,
            HttpSession session
    ) {
        //获取以登录用户的手机号
        String mobile = (String) session.getAttribute("user");
        orders.setPhone(mobile);
        orders.setCreatetime(new Date());
        return R.ok().put("order", this.ordersService.save(orders));
    }

    /**
     * 修改数据
     *
     * @param orders 实体对象
     * @return 修改结果
     */
    @PutMapping
    public R update(@RequestBody Orders orders) {
        return R.ok().put("order", this.ordersService.updateById(orders));
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @DeleteMapping
    public R delete(@RequestParam("idList") List<Long> idList) {
        return R.ok().put("order", this.ordersService.removeByIds(idList));
    }
}